<template>
    <div>
        <!--三级分类-->
        <Category :scene="scene"></Category>
        <el-card style="margin: 10px 0;">
            <div v-show="scene == 0">
                <el-button @click="addSpu" type="primary" icon="Plus" :disabled="!categoryStore.c3Id">添加SPU</el-button>
                <!--展示已有的spu数据-->
                <el-table style="margin: 10px 0;" :border="true" :data="records">
                    <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
                    <el-table-column label="SPU名称" prop="spuName"></el-table-column>
                    <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
                    <el-table-column label="SPU操作">
                        <template #default="scope">
                            <el-button type="primary" size="small" icon="Plus" title="添加SKU"
                                @click="addSku(scope.row)"></el-button>
                            <el-button type="primary" size="small" icon="Edit" title="修改SPU"
                                @click="updateSpu(scope.row)"></el-button>
                            <el-button type="primary" size="small" icon="View" title="查看SKU列表"
                                @click="findSku(scope.row)"></el-button>
                            <el-popconfirm :title="`您确定要删除${scope.row.spuName}吗？`" width="200px" @confirm="deleteSpu(scope.row.id)">
                                <template #reference>
                                    <el-button type="danger" size="small" icon="Delete" title="删除SPU"></el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
                    :background="true" layout="prev, pager, next, jumper,->,sizes,total " :total="total"
                    @current-change="gethasSpu" @size-change="changeSize" />
            </div>
            <!--添加或修改SPU-->
            <spuForm ref="spuFormRef" v-show="scene == 1" @changeScene="changeScene"></spuForm>
            <!--添加SKU-->
            <skuForm ref="skuFormRef" v-show="scene == 2" @changeScene="changeScene"></skuForm>
            <!--dialog对话框：展示已有的sku数据-->
            <el-dialog v-model="show" title="SKU列表">
                <el-table :data="skuArr" border :style="{ width: '100%' }">
                    <el-table-column prop="skuName" label="SKU名字" width="180" />
                    <el-table-column prop="price" label="SKU价格" width="180" />
                    <el-table-column prop="weight" label="SKU重量" />
                    <el-table-column label="SKU图片">
                        <template #="{ row }">
                            <img :src="row.skuDefaultImg" alt="暂无图片" style="width: 100px; height: 100px">
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue';
//引入分类的仓库
import useCategoryStore from '@/store/modules/category';
import { reqHasSpu, reqRemoveSpu, reqSkuList } from '@/api/product/spu';
import type { HasSpuResponseData, Records, SkuData, SkuInfoData, SpuData } from '@/api/product/spu/type';
import spuForm from './spuForm.vue';
import skuForm from './skuForm.vue';
import { ElMessage } from 'element-plus';

let categoryStore = useCategoryStore();

let scene = ref(0)  //定义card组件内容切换：0为已有的SPU，1为添加或修改SPU，2为添加SKU
let pageNo = ref(1)
let pageSize = ref(3)
//存储已有的spu的数据
let records = ref<Records>([])
//存储spu总数
let total = ref(0)
let spuFormRef = ref()
let skuFormRef = ref()
let skuArr = ref<SkuData[]>([])
let show = ref(false)
//监听三级分类ID变化
watch(() => categoryStore.c3Id, () => {
    //务必保证有三级分类ID
    if (!categoryStore.c3Id) return;
    gethasSpu()
})

//此方法执行：可以获取某一个三级分类下全部的已有的SPU
const gethasSpu = async (pager = 1) => {
    console.log('====', pager)
    pageNo.value = pager
    let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id);
    if (result.code === 200) {
        records.value = result.data.records;
        total.value = result.data.total
    }
}

//改变每页显示的条数
const changeSize = () => {
    gethasSpu()
}

//添加SPU
const addSpu = () => {
    scene.value = 1
    spuFormRef.value.initAddSpu(categoryStore.c3Id)
}
//子组件spuForm绑定自定义事件
const changeScene = (obj: { num: number, flag: string }) => {
    scene.value = obj.num
    gethasSpu(obj.flag === 'add' ? 1 : pageNo.value)
}
const updateSpu = (row: SpuData) => {
    scene.value = 1
    spuFormRef.value.initHasSpuData(row)
}

const addSku = (row: SpuData) => {
    scene.value = 2
    skuFormRef.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

const findSku = async (row: SpuData) => {
    let result: SkuInfoData = await reqSkuList(row.id as number)
    if (result.code === 200) {
        skuArr.value = result.data
        show.value = true
    } else {
        ElMessage.error(result.message)
    }
}

const deleteSpu = (id: number) => {
    reqRemoveSpu(id).then((result) => {
        if (result.code === 200) {
            ElMessage.success('删除成功')
            gethasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
        } else {
            ElMessage.error(result.message)
        }
    })
}

onBeforeUnmount(() => {
    categoryStore.$reset()
})
</script>

<style scoped></style>